package org.rails.extjs.panel.grid.column;

import org.rails.extjs.javascript.Function;
import org.rails.extjs.javascript.JavaScript;
import org.rails.extjs.panel.grid.CheckboxSelectionModel;
import org.rails.extjs.panel.grid.GridPanel;
import org.rails.extjs.panel.grid.column.Column;

public class CheckboxColumn extends Column {

	private GridPanel grid;
	
	public CheckboxColumn(GridPanel grid) {
		super();
		this.grid = grid;
	}
	
	@Override
	public String getScript(){		
		final CheckboxSelectionModel csm = (CheckboxSelectionModel) grid.getSelectModel();
		final JavaScript js = new JavaScript("var grid=Ext.getCmp('{0}');",grid.getId());
		js.append("var row = e.getTarget('.x-grid3-row');");
		js.append("var index = row.rowIndex;");
		js.append("var selMod = grid.getSelectionModel();");
		js.append("if(selMod && selMod.isSelected(index)){");
		js.append("selMod.deselectRow(index);");
		js.append("}else{");
		js.append("selMod.selectRow(index, true);");
		js.append("selMod.grid.getView().focusRow(index);");
		js.append("}");
		csm.addOption("onMouseDown",new Function(js,"e,t"));
		return csm.getScript();		
	}

}
